#include "../../common/mysql_chat_session_member.hpp"
#include "../../common/utils.hpp"
std::shared_ptr<odb::core::database> db;
void insert_test() {
    ChatSessionMemberTable::ptr csm_table = std::make_shared<ChatSessionMemberTable>(db);
    ChatSessionMember csm1("1", "1001");
    ChatSessionMember csm2("1", "1002");
    ChatSessionMember csm3("2", "1003");

    // 测试单个插入
    // if (csm_table->append(csm1)) {
    //     std::cout << "单个插入成功: chat_session_id=1, user_id=1001" << std::endl;
    // } else {
    //     std::cout << "单个插入失败" << std::endl;
    // }

    // 测试批量插入
    if (csm_table->append({csm2, csm3})) {
        std::cout << "批量插入成功: chat_session_id=1, user_id=1002; chat_session_id=2, user_id=1003" << std::endl;
    } else {
        std::cout << "批量插入失败" << std::endl;
    }
}

void remove_test() {
    ChatSessionMemberTable::ptr csm_table = std::make_shared<ChatSessionMemberTable>(db);
    // 测试删除
    ChatSessionMember csm1("1", "1003");
    if (csm_table->remove(csm1)) {
        std::cout << "删除成功: chat_session_id=1, user_id=1001" << std::endl;
    } else {
        std::cout << "删除失败: chat_session_id=1, user_id=1001" << std::endl;
    }
    ChatSessionMember csm2("2", "1003");
    csm_table->remove(csm2);
    std::cout << "删除成功: chat_session_id=2, user_id=1003" << std::endl;

    // 批量删除
    if (csm_table->remove("1")) {
        std::cout << "批量删除成功: chat_session_id=1, user_id=1002; chat_session_id=2, user_id=1003" << std::endl;
    } else {
        std::cout << "批量删除失败" << std::endl;
    }
}


void remove_not_exists_test() {
    ChatSessionMemberTable::ptr csm_table = std::make_shared<ChatSessionMemberTable>(db);
    // 测试删除不存在的记录
    ChatSessionMember csm1("3", "1001");
    if (csm_table->remove(csm1)) {
        std::cout << "删除成功: chat_session_id=3, user_id=1001" << std::endl;
    } else {
        std::cout << "删除失败: chat_session_id=3, user_id=1001" << std::endl;
    }
}

void selece_test() {
    ChatSessionMemberTable::ptr csm_table = std::make_shared<ChatSessionMemberTable>(db);
    // 测试查询
    auto result = csm_table->members("sessino1");

    if(result.empty()) {
        std::cout << "查询结果为空" << std::endl;
    } else {
        std::cout << "查询结果: " << std::endl;
        for (const auto& member : result) {
            std::cout << "user_id: " << member << std::endl;
        }
    }

    result = csm_table->members("10");
    if(result.empty()) {
        std::cout << "查询结果为空" << std::endl;
    } else {
        std::cout << "查询结果: " << std::endl;
        for (const auto& member : result) {
            std::cout << "user_id: " << member << std::endl;
        }
    }
}
int main() {
    init_logger();
    db = ODBFactory::create(
        "root",
        "&a==nullPTR1",
        "chat_system"
    );
    // insert_test();
    // remove_test();
    // remove_not_exists_test();
    selece_test();
    return 0;
}